package com.tiantian.quanminxiu.dialog;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.jakewharton.rxbinding.view.RxView;
import com.nineoldandroids.animation.Animator;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.adapter.UserListAdapter;
import com.tiantian.quanminxiu.presenter.DialogUserListPresent;
import com.tiantian.quanminxiu.view.DialogUserlistView;

import org.json.JSONArray;

import rx.functions.Action1;

public class Dialog_UserList implements DialogUserlistView{

    //region 初始化数据
    private final int USERLIST = 1;
    private final int ADMINLIST = 2;
    private final int BLACKLIST = 1;

    private static Dialog_UserList dialog_giftList;
    private PopupWindow popupWindow;
    private Activity activity;
    private String Anchor_id;

    private DialogUserListPresent present;

    private View vMain,vLoading;
    private RecyclerView mRecyclerView;
    private TextView txtUserList,txtAdminList,txtBlackList;
    private UserListAdapter adapter;

    private int CurrentType = USERLIST;        // 当前选择类型
    //endregion

    //region onCreate
    private Dialog_UserList(Activity activity, String anchor_id) {
        this.activity = activity;
        this.Anchor_id = anchor_id;
        View view = View.inflate(activity,R.layout.dialog_userlist,null);
        initPopupWindow(view);
        InitView(view);

        present = new DialogUserListPresent();
        present.attachView(this);
        present.getUserList();
    }
    //endregion

    public static Dialog_UserList getInstance(Activity activity,String anchor_id){
        if(dialog_giftList==null){
            dialog_giftList = new Dialog_UserList(activity,anchor_id);
        }
        return dialog_giftList;
    }

    //region 关闭窗口 closeWindow
    public void closeWindow(){
        popupWindow.dismiss();
        present.detachView();
        dialog_giftList = null;
    }
    //endregion

    //region 显示窗口 show(View view)
    public void show(View view){
        if (null != popupWindow) {
            popupWindow.showAtLocation(view, Gravity.BOTTOM
                    | Gravity.CENTER_HORIZONTAL, 0, 0);
        }
    }
    //endregion

    //region 是否展示 isShow
    public boolean isShow(){
        if(popupWindow==null){
            return false;
        }
        return popupWindow.isShowing();
    }
    //endregion

    //region 初始化popwindow initPopupWindow(View view)
    public void initPopupWindow(View view){
        popupWindow = new PopupWindow();
        // 设置SelectPicPopupWindow弹出窗体的宽
        popupWindow.setWidth(ActionBar.LayoutParams.FILL_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        popupWindow.setHeight(ActionBar.LayoutParams.FILL_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        popupWindow.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        popupWindow.setAnimationStyle(R.style.AnimBottom);
        popupWindow.setContentView(view);
        popupWindow.setOutsideTouchable(true);

        view.setFocusable(true);//comment by danielinbiti,设置view能够接听事件，标注1
        view.setFocusableInTouchMode(true); //comment by danielinbiti,设置view能够接听事件 标注2
        view.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                if (arg1 == KeyEvent.KEYCODE_BACK) {
                    if (popupWindow != null) {
                        closeWindow();
                    }
                }
                return false;
            }
        });
    }
    //endregion

    //region InitView(View view)
    public void InitView(View view) {
        vMain = view.findViewById(R.id.vMain);
        vLoading = view.findViewById(R.id.vLoading);
        txtUserList = (TextView) view.findViewById(R.id.txtUserList);
        txtAdminList = (TextView) view.findViewById(R.id.txtAdminList);
        txtBlackList = (TextView) view.findViewById(R.id.txtBlackList);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.mRecyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        RxView.clicks(vMain).map(View->vMain).subscribe(OC_Button);
        RxView.clicks(txtUserList).map(View->txtUserList).subscribe(OC_Button);
        RxView.clicks(txtAdminList).map(View->txtAdminList).subscribe(OC_Button);
        RxView.clicks(txtBlackList).map(View->txtBlackList).subscribe(OC_Button);
    }
    //endregion

    //region 关闭窗口 OC_Button
    private Action1<View> OC_Button = v->{
        switch (v.getId()){
            case R.id.vMain:
                closeWindow();
                break;
            case R.id.txtUserList:
                present.getUserList();
                CurrentType = USERLIST;
                break;
            case R.id.txtAdminList:
                present.getAdminList();
                CurrentType = ADMINLIST;
                break;
            case R.id.txtBlackList:
                present.getBlackList();
                CurrentType = BLACKLIST;
                break;
        }
    };


    @Override
    public Context getContext() {
        return activity;
    }

    @Override
    public void Loading_Open() {
        YoYo.with(Techniques.FadeIn).duration(200).playOn(vLoading);
        vLoading.setVisibility(View.VISIBLE);
    }

    @Override
    public void Loading_Open(boolean flag) {

    }

    @Override
    public void Loading_Close() {
        if (vLoading.getVisibility() == View.GONE) {
            return;
        }
        YoYo.with(Techniques.FadeOut).duration(700).withListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                vLoading.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        }).playOn(vLoading);
    }

    @Override
    public void getUserList(JSONArray userArray) {
        adapter = new UserListAdapter(activity,userArray);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    public void getAdminList(JSONArray amdinArray) {
        adapter = new UserListAdapter(activity,amdinArray);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    public void getBlackList(JSONArray blackArray) {
        adapter = new UserListAdapter(activity,blackArray);
        mRecyclerView.setAdapter(adapter);
    }
    //endregion
}